﻿using System;
using System.Collections.Generic;

namespace WPFConsole
{
    public partial class PConsole
    {
        void ConnectCommand(string[] args)
        {
            string ip = "";
            try
            {
                ip = args[Array.IndexOf(args, "-connect") + 1];
            }
            catch (IndexOutOfRangeException)
            {
                WriteLine("Error processing command-line information for -connect");
            }
            PreMadeInput("connect " + ip);
            if (!((IList<string>) args).Contains("-login") || _needToStopParsingArgs) return;
            _needToStopParsingArgs = true;
            LoginCommand(args);
        }

        void LoginCommand(string[] args)
        {
            string uname = "";
            string pass = "";
            try
            {
                uname = args[Array.IndexOf(args, "-login") + 1];
                pass = args[Array.IndexOf(args, "-login") + 2];
            }
            catch (IndexOutOfRangeException)
            {
                WriteLine("Error processing command-line information for -login");
            }
            if (((IList<string>)args).Contains("-connect") && _needToStopParsingArgs)
            {
                PreMadeInput(("login " + uname + " " + pass));
            }
            else if (((IList<string>)args).Contains("-connect") && !_needToStopParsingArgs)
            {
                ConnectCommand(args);
            }
            else if (!((IList<string>)args).Contains("-connect"))
            {
                WriteLine("Cannot use the login command line argument without the connect argument.");
            }
        }
    }
}
